import Vue from 'vue'
import VueRouter from 'vue-router'
import VueCookies from 'vue-cookies'
import store from '@/store'

Vue.use(VueCookies)
Vue.use(VueRouter)
import Backend from '@/components/Backend'
import Account from '@/components/Account'
import Home from '@/pages/Home'
import News from '@/pages/News'
import Search from '@/pages/Search'
import UserInfo from '@/pages/UserInfo'
import Longin from '@/pages/Longin'
import Register from '@/pages/Register'
import ArticleList from '@/pages/ArticleList'
import PersonalInfo from '@/pages/UserInfo/Bottom/PersonalInfo'
import EditAvatar from '@/pages/UserInfo/Bottom/EditAvatar'
import Article from '@/pages/Article'
import AddArticle from '@/pages/AddArticle'
import Index from "@/pages/Index";

let originPush = VueRouter.prototype.push;
let originreplace = VueRouter.prototype.replace;

// 重写push
VueRouter.prototype.push = function (location, resolve, rereject) {
    if (resolve && rereject) {
        originPush.call(this, location, resolve, rereject)
    } else {
        originPush.call(this, location, () => {
        }, () => {
        })
    }
}
// 重写replace
VueRouter.prototype.replace = function (location, resolve, rereject) {
    if (resolve && rereject) {
        originreplace.call(this, location, resolve, rereject)
    } else {
        originreplace.call(this, location, () => {
        }, () => {
        })
    }
}


let router;
export default router = new VueRouter({
    routes: [
        // 重定向到首页
        {
            path: '',
            redirect: '/home',
        },
        // 首页路由
        {
            name: 'index',
            path: '',
            component: Index,
            children: [
                {
                    path: 'home',
                    component: Home,
                    //前置独享守卫
                    beforeEnter(to, from, next) {
                        // 如果是从登录注册等路由跳转过来则对用户信息进行更新
                        if (from.meta.title === 'Account') {
                            let userinfo = Vue.$cookies.get('userinfo')
                            store.commit('setUserInfo', userinfo)
                        }
                        next()
                    },
                    name: 'home',
                    meta: {
                        type: 1
                    }
                },
                // 新闻路由
                {
                    path: 'news',
                    component: News,
                    meta: {
                        type: 2
                    },
                    name: 'news'
                },
                // 搜索路由
                {
                    path: 'search',
                    component: Search,
                    name: 'search',
                },
                // 文章详情
                {
                    path: '/article/:id',
                    component: Article,
                    name: 'article',
                    meta: {
                        type: 3
                    }
                },
            ]
        },
        // 登录注册等路由
        {
            path: '/account',
            component: Account,
            meta: {
                title: 'Account',
            },
            children: [
                // 重定向到登录页面
                {
                    path: '',
                    redirect: 'login',
                },
                // 登录页面
                {
                    path: 'login',
                    component: Longin,
                    meta: {
                        title: 'Account',
                    },
                    name: 'login',
                },
                // 注册页面
                {
                    path: 'register',
                    component: Register,
                    meta: {
                        title: 'Account',
                    },
                    name: 'register'
                },
            ],
        },
        // 个人中心路由
        {
            path: '/backend',
            component: Backend,
            children: [
                // 重定向个人资料页面
                {
                    path: '',
                    redirect: 'userinfo'
                },
                // 个人资料页面
                {
                    path: 'userinfo',
                    component: UserInfo,
                    children: [
                        // 完善信息页面
                        {
                            path: 'personalinfo',
                            component: PersonalInfo,
                        },
                        // 修改头像页面
                        {
                            path: 'editavatar',
                            component: EditAvatar,
                        }
                    ],
                    name: 'UserInfo',
                    meta: {
                        title: 'backend',
                    }
                },
                // 文章管理
                {
                    path: 'articleList',
                    component: ArticleList,
                    meta: {
                        title: 'backend',
                    },
                    name:'ArticleList'
                },
                // 添加博客
                {
                    path: 'addarticle',
                    component: AddArticle,
                    meta: {
                        name: '完善文章信息',
                        title: 'backend',
                    },
                    name:'AddArticle'
                },
                // 编辑博客
                {
                    path: 'editarticle/:id',
                    component: AddArticle,
                    meta: {
                        name: '编辑文章信息',
                        title: 'backend',
                    },
                    name: 'EditArticle'
                },
            ],
        },
    ],
})
//全局前置钩子
router.beforeEach((to, from, next) => {
    if (Vue.$cookies.get('userinfo') === null && to.meta.title === 'backend') {
        to.meta.show = true
        router.replace({
            name: 'login'
        })
    }
    next()
})

//全局后置钩子
router.afterEach((to, from) => {
    // 获取轮播图节点
    let img = document.getElementById(`img`)
    if (img) {
        img.scrollIntoView()
    }
})

